package com.lkm.market2_0.help;

import com.lkm.frame.task2.Task;
import com.lkm.market2_0.ExpandConfig;

import java.util.ArrayList;
import java.util.List;

public class TaskHelp {

    public static <T extends Task> T getTask(String taskId) {
        int[] runs = ExpandConfig.instance.getTaskManager2().getRunTask(taskId);
        if (runs != null && runs.length != 0) {
            Task task = null;
            for (int se : runs) {
                task = ExpandConfig.instance.getTaskManager2().getRunTask(se);
                if (task != null) {
                    return (T) task;
                }
            }
        }
        return null;
    }

    public static <T extends Task> List<T> getAllTask(String taskId) {
        int[] runs = ExpandConfig.instance.getTaskManager2().getRunTask(taskId);
        if (runs != null && runs.length != 0) {
            List<T> tasks = new ArrayList<>(runs.length);
            Task task = null;
            for (int se : runs) {
                task = ExpandConfig.instance.getTaskManager2().getRunTask(se);
                if (task != null) {
                    tasks.add((T) task);
                }
            }

            return tasks;
        }
        return null;
    }

    public static void cancelById(String taskId) {
        int[] runs = ExpandConfig.instance.getTaskManager2().getRunTask(taskId);
        if (runs != null && runs.length != 0) {
            Task task = null;
            for (int se : runs) {
                task = ExpandConfig.instance.getTaskManager2().getRunTask(se);
                if (task != null) {
                    task.cancel();
                }
            }


        }

    }
}
